<?php

namespace app\common\service;
use think\Config;
use baidu_face\AipFace;

/**
 * 调用百度人脸接口
 *
 * @author jiang.ding
 */
class BaiduService {

    /**
     * 初始化
     */
    public function __construct() {
        # 创建百度人脸识别对象
        $appId = Config::get('BAIDU_FACE_APP_ID');
        $apiSecret = Config::get('BAIDU_FACE_API_SECRET');
        $secretKey = Config::get('BAIDU_FACE_SECRET_KEY');
        $this->_baiduAip = new AipFace($appId, $apiSecret, $secretKey);
    }

    /**
     * 人脸检测
     * @param $data array 图片地址url
     */
    public function detect($data) {
        $image = @file_get_contents($data['url']);
        $options = ['face_fields' => 'age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities'];
        $result = $this->_baiduAip->detect($image, $options);

        return $result;
    }

    /**
     * 人脸识别
     * @param $data array 含分组id 和图片地址
     */
    public function identifyUser($data) {

        $images = file_get_contents($data['url']);
        $result = $this->_baiduAip->identifyUser($data['groupId'], $images);
        return $result;
    }

    /**
     * 人脸注册
     * @param $data array 其中含uid,url,userInfo,groupId
     * @return $res array 结果集
     */
    public function addBaiduFace($data) {
        $image = file_get_contents($data['url']);
        $res = $this->_baiduAip->addUser($data['uid'], $data['userInfo'],
                $data['groupId'], $image);

        return $res;
    }

    /**
     * 人脸删除 
     * @param $uid type string 图片ID 
     * @param $options type array 分组ID 
     * @return type object 删除信息
     */
    public function deleteUser($uid, $options) {
        $res = $this->_baiduAip->deleteUser($uid, $options);
        return $res;
    }

}
